package cave.ui;

import java.util.ArrayList;
import android.graphics.Color;
import android.util.FloatMath;
import cave.game.InputEvent;
import cave.renderer.RenderList;
import cave.renderer.Texture;

public class RadioButton {
	
	private float x, y;
	private float width, height;
	private float red, green, blue, alpha;
	private float[] orientation;
	private RadioButtonGroup group;
	private boolean selected, enabled, pressed;
	private int pointerId, index;
	private Texture texChecked, texNotChecked, texPressed, texDisabled;
	
	public RadioButton(float x, float y, float width, float height, Texture texChecked,
			Texture texNotChecked, Texture texPressed, Texture texDisabled) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.texChecked = texChecked;
		this.texNotChecked = texNotChecked;
		this.texPressed = texPressed;
		this.texDisabled = texDisabled;
		this.selected = false;
		this.group = null;
		this.red = 1.0f;
		this.green = 1.0f;
		this.blue = 1.0f;
		this.alpha = 1.0f;
		orientation = new float[2];
		orientation[0] = 0.0f;
		orientation[1] = 1.0f;
		enabled = true;
		pointerId = -1;
		index = -1;
		texChecked.incUsers();
		texNotChecked.incUsers();
		texPressed.incUsers();
		texDisabled.incUsers();
	}
	
	public void update(float dt, ArrayList<InputEvent> events) {
		if (enabled) {
			InputEvent event;
			for (int i = 0; i < events.size(); i++) {
				event = events.get(i);
				if (!event.isInUse()) {
					switch (event.getType()) {
					case UP:
						if (pressed && pointerId == event.getPointerId() &&
								event.getX() > x && event.getX() < x + width &&
								event.getY() > y && event.getY() < y + height) {
							if (group != null) {
								group.setSelected(index);
							} else {
								selected = !selected;
							}
							pointerId = -1;
							pressed = false;
						}
						break;
						
					case DOWN:
						if (!pressed &&
								event.getX() > x && event.getX() < x + width &&
								event.getY() > y && event.getY() < y + height) {
							pointerId = event.getPointerId();
							pressed = true;
						}
						break;
						
					case MOVE:
						if (pressed && pointerId == event.getPointerId() && !(
								event.getX() > x && event.getX() < x + width &&
								event.getY() > y && event.getY() < y + height)) {
							pressed = false;
							pointerId = -1;
						}
						break;
					}
				}
			}
		}
		
	}

	public void draw(RenderList renderList) {
		if (!enabled) {
			renderList.addScreenData(x, y, width, height, orientation[0], orientation[1],
					texDisabled, red, green, blue, alpha);
		} else if (pressed) {
			renderList.addScreenData(x, y, width, height, orientation[0], orientation[1],
					texPressed, red, green, blue, alpha);
		} else if (selected) {
			renderList.addScreenData(x, y, width, height, orientation[0], orientation[1],
					texChecked, red, green, blue, alpha);
		} else {
			renderList.addScreenData(x, y, width, height, orientation[0], orientation[1],
					texNotChecked, red, green, blue, alpha);
		}
	}

	public void destroy() {
		texChecked.decUsers();
		texNotChecked.decUsers();
		texPressed.decUsers();
		texDisabled.decUsers();
	}
	
	public void setSelected(boolean checked) {
		this.selected = checked;
	}
	
	public boolean isSelected() {
		return selected;
	}
	
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}
	
	public void setOrientation(float x, float y) {
		orientation[0] = x; orientation[1] = y;
	}
	
	public void setOrientation(float radians) {
		orientation[0] = FloatMath.cos(radians + (float)Math.PI/2);
		orientation[1] = FloatMath.sin(radians + (float)Math.PI/2);
	}
	
	public float[] getOrientation() {
		return orientation;
	}
	
	public void setPosition(float x, float y) {
		this.x = x;
		this.y = y;
	}
	
	public float getX() {
		return x;
	}
	
	public float getY() {
		return y;
	}
	
	public void setX(float x) {
		this.x = x;
	}
	
	public void setY(float y) {
		this.y = y;
	}
	
	public float getWidth() {
		return width;
	}
	
	public float getHeight() {
		return height;
	}
	
	public void setWidth(float width) {
		this.width = width;
	}
	
	public void setHeight(float height) {
		this.height = height;
	}
	
	public void setTextures(Texture texChecked, Texture texNotChecked,
			Texture texPressed, Texture texDisabled) {
		this.texChecked = texChecked;
		this.texNotChecked = texNotChecked;
		this.texPressed = texPressed;
		this.texDisabled = texDisabled;
	}
	
	public void setColor(int color) {
		red = Color.red(color) / 255.0f;
		green = Color.red(color) / 255.0f;
		blue = Color.red(color) / 255.0f;
		alpha = Color.red(color) / 255.0f;
	}
	
	public void setColor(float red, float green, float blue, float alpha) {
		this.red = red;
		this.green = green;
		this.blue = blue;
		this.alpha = alpha;
	}
	
	public void setRadioButtonGroup(RadioButtonGroup group) {
		this.group = group;
	}
	
	public void setIndex(int index) {
		this.index = index;
	}
		
}
